from django.shortcuts import render

# Create your views here.

#判断用户输入的用户名在数据库中是否存在
from rest_framework import status
from rest_framework.generics import GenericAPIView, RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

# from mall.apps.users.models import User
from users.models import User

from users import serializers


class UsernameView(APIView):
    def get(self,request,username):

        # '/users/usernames/' + this.username + '/count/',获取参数
        count = User.objects.filter(username=username).count()

        #返回数据
        data={
            "count":count
        }
        return Response(data,status=status.HTTP_200_OK)
        pass
    pass

class MobileView(APIView):
    def get(self,request,mobile):

        # /users/phones/'+ this.mobile + '/count/,获取参数
        count = User.objects.filter(mobile=mobile).count()

        #返回数据
        data={
            "count":count
        }
        return Response(data,status=status.HTTP_200_OK)
        pass
    pass

class RegisterView(GenericAPIView):
    serializer_class = serializers.UserRegisterSerializer

    #可以用mixin + GenericAPIView实现
    #也可以用CreateAPIView实现
    def post(self,request):
        #获取json参数
        json_data = request.data

        #生成一个序列化器,校验入库
        serializer = self.get_serializer(data=json_data)
        serializer.is_valid(raise_exception=True)

        #数据入库
        serializer.save()

        #返回响应
        return Response(serializer.data,status=status.HTTP_201_CREATED)

    pass

#根本就没有写类视图
# class LoginView(GenericAPIView):
#     serializer_class = serializers.UserLoginSerilizer
#     def get(self,request):
#         json_data = request.data
#         serializer = self.get_serializer(data=json_data)
#         serializer.is_valid(raise_exception=True)
#         return Response(serializer.data)
#     pass
class UserInfoView(RetrieveAPIView):
    serializer_class = serializers.UserInfoSerializer
    permission_classes = [IsAuthenticated]

    def get_object(self):
        return self.request.user
    #也可以使用mixin+GenericAPIView实现,return self.retrieve(request)
    #也可以用三级视图:RetrieveAPIView实现
    # def get(self,request):
    #     #获取数据
    #     user = self.get_object()
    #
    #     #使用序列化器
    #     serializers = self.get_serializer(instance=user)
    #
    #     #返回数据
    #     return Response(serializers.data)
    #     pass
    # pass


class SendEmailView(GenericAPIView):
    serializer_class = serializers.SendEmailSerializer
    permission_classes = [IsAuthenticated]  # 普通用户验证

    def get_object(self):
        return self.request.user  # 重写get_object
    #也可以使用mixin+GenericAPIView实现,return self.retrieve(request)
    #也可以用三级视图:RetrieveAPIView实现
    def put(self, request):
        # 获取参数
        email = request.data
        user = self.get_object()

        # 创建序列化器
        serializer = self.get_serializer(instance=user, data=email)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        # 返回响应
        return Response(serializer.data)
        pass
    pass